package com.distribute.portal.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.ObjectUtils.Null;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.distribute.common.pojo.DistributeResult;
import com.distribute.common.util.HttpClientUtil;
import com.distribute.common.util.JsonUtils;
import com.distribute.pojo.DbContent;
import com.distribute.portal.service.ContentService;

/**
 * 调用服务层服务，查询内容列表
 * <p>
 * Title: ContentServiceImpl
 * </p>
 * 
 * @version 1.0
 *          内容展示流程：浏览器请求首页--distribute-portal--distribute-portal请求distribute-rest服务返回内容数据并合并到html返回--浏览器渲染首页（大轮播图等）
 */
@Service
public class ContentServiceImpl implements ContentService {

	@Value("${REST_BASE_URL}")
	private String REST_BASE_URL;
	@Value("${REST_INDEX_AD_URL}")
	private String REST_INDEX_AD_URL;
	@Value("${REST_INDEX_AD_URL2}")
	private String REST_INDEX_AD_URL2;
	@Value("${REST_INDEX_AD_URL3}")
	private String REST_INDEX_AD_URL3;
	@Value("${REST_INDEX_AD_URL4}")
	private String REST_INDEX_AD_URL4;

	@Override
	public String getContentList() {
		// 调用服务层的服务
		String result = HttpClientUtil.doGet(REST_BASE_URL + REST_INDEX_AD_URL);
		// 把字符串转换成distributeResult
		try {
			DistributeResult distributeResult = DistributeResult.formatToList(result, DbContent.class);
			// 取内容列表
			List<DbContent> list = (List<DbContent>) distributeResult.getData();
			List<Map> resultList = new ArrayList<>();
			if (list != null) {
				// 创建一个jsp页码要求的pojo列表
				for (DbContent dbContent : list) {
					Map map = new HashMap<>();
					map.put("src", dbContent.getPic());
					map.put("title", dbContent.getTitle());
					map.put("subTitle", dbContent.getSubTitle());
					map.put("titleDesc", dbContent.getTitleDesc());
					map.put("height", 388);
					map.put("width", 756);
					map.put("srcB", dbContent.getPic2());
					map.put("widthB", 550);
					map.put("heightB", 240);
					map.put("href", dbContent.getUrl());
					map.put("alt", dbContent.getSubTitle());
					resultList.add(map);
				}
			}
			// 返回的是json字符串
			return JsonUtils.objectToJson(resultList);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	@Override
	public String getContentList2() {
		// 调用服务层的服务
		String result = HttpClientUtil.doGet(REST_BASE_URL + REST_INDEX_AD_URL2);
		// 把字符串转换成distributeResult
		try {
			DistributeResult distributeResult = DistributeResult.formatToList(result, DbContent.class);
			// 取内容列表
			List<DbContent> list = (List<DbContent>) distributeResult.getData();
			List<Map> resultList = new ArrayList<>();
			if (list != null) {
				// 创建一个jsp页码要求的pojo列表
				for (DbContent dbContent : list) {
					Map map = new HashMap<>();
					map.put("src", dbContent.getPic());
					map.put("title", dbContent.getTitle());
					map.put("subTitle", dbContent.getSubTitle());
					map.put("titleDesc", dbContent.getTitleDesc());
					map.put("height", 388);
					map.put("width", 756);
					map.put("srcB", dbContent.getPic2());
					map.put("widthB", 550);
					map.put("heightB", 240);
					map.put("href", dbContent.getUrl());
					map.put("alt", dbContent.getSubTitle());
					resultList.add(map);
				}
			}
			// 返回的是json字符串
			return JsonUtils.objectToJson(resultList);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	@Override
	public String getContentList3() {
		// 调用服务层的服务
		String result = HttpClientUtil.doGet(REST_BASE_URL + REST_INDEX_AD_URL3);
		// 把字符串转换成distributeResult
		try {
			DistributeResult distributeResult = DistributeResult.formatToList(result, DbContent.class);
			// 取内容列表
			List<DbContent> list = (List<DbContent>) distributeResult.getData();
			List<Map> resultList = new ArrayList<>();
			if (list != null) {
				// 创建一个jsp页码要求的pojo列表
				for (DbContent dbContent : list) {
					Map map = new HashMap<>();
					map.put("src", dbContent.getPic());
					map.put("title", dbContent.getTitle());
					map.put("subTitle", dbContent.getSubTitle());
					map.put("titleDesc", dbContent.getTitleDesc());
					map.put("height", 388);
					map.put("width", 756);
					map.put("srcB", dbContent.getPic2());
					map.put("widthB", 550);
					map.put("heightB", 240);
					map.put("href", dbContent.getUrl());
					map.put("alt", dbContent.getSubTitle());
					resultList.add(map);
				}
			}
			// 返回的是json字符串
			return JsonUtils.objectToJson(resultList);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	@Override
	public String getContentList4() {
		// 调用服务层的服务
		String result = HttpClientUtil.doGet(REST_BASE_URL + REST_INDEX_AD_URL4);
		// 把字符串转换成distributeResult
		try {
			DistributeResult distributeResult = DistributeResult.formatToList(result, DbContent.class);
			// 取内容列表
			List<DbContent> list = (List<DbContent>) distributeResult.getData();
			List<Map> resultList = new ArrayList<>();
			if (list != null) {
				// 创建一个jsp页码要求的pojo列表
				for (DbContent dbContent : list) {
					Map map = new HashMap<>();
					map.put("src", dbContent.getPic());
					map.put("title", dbContent.getTitle());
					map.put("subTitle", dbContent.getSubTitle());
					map.put("titleDesc", dbContent.getTitleDesc());
					map.put("height", 388);
					map.put("width", 756);
					map.put("srcB", dbContent.getPic2());
					map.put("widthB", 550);
					map.put("heightB", 240);
					map.put("href", dbContent.getUrl());
					map.put("alt", dbContent.getSubTitle());
					resultList.add(map);
				}
			}
			// 返回的是json字符串
			return JsonUtils.objectToJson(resultList);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

}
